package model;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;

import controller.Comment;
import sql.Db;
import sql.ResultSetHandler;

public class PostModel {

    public String text, authorName;
    public String authorId, postId, targetId;
    public Date date;
    public ArrayList<Comment> comments = new ArrayList<Comment>();

    /**
     * Construct a new post model
     */
    public PostModel() {
    	text = "";
    	authorName = "";
    	authorId = "";
    	postId = "";
    	targetId = "";
    	date = null;
    }

    // Delete this post from the db
    public void remove() {
        Db db = Db.getDb();
        db.executeNonQuery("DELETE FROM post WHERE id = " + postId, new ResultSetHandler() {
            @Override
            public void handleResult(ResultSet rs) {
                postId = ""; // clear id since we don't really exist anymore
            }
        });
    }

    /**
     * Construct a post model from a result set.
     * @param rs
     */
    public PostModel (ResultSet rs) {

        try {
            text = rs.getString("text");
            authorName = rs.getString("name");

            authorId = rs.getString("authorID");
            targetId = rs.getString("targetID");
            postId = rs.getString("id");

            date = rs.getTimestamp("timestamp");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Commit this model to the db. If ID exists, then it's an update, otherwise it's an insert.
     */
    public void commit() {
    	Db my_db = Db.getDb();
    	try {
            if (postId.equals("")) {
            	my_db.executeNonQuery(
                    "INSERT INTO post(authorID, targetID, timestamp, text)"+
                    "\nVALUES('" +
                        authorId + "', '" +
                        targetId + "', '" +
                        new java.sql.Timestamp(this.date.getTime()) + "', '" +
                        text + "')",

                    new ResultSetHandler() {
                        @Override
                        public void handleResult(ResultSet rs) {
                            try {
                                if (rs.next()) {
                                    postId = rs.getString(1);
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                );
            } else {
            	my_db.executeNonQuery("UPDATE post SET " +
                    "authorID = " + authorId +
                    ", targetID = " + targetId +
                    ", timestamp = " + date.toString() +
                    ", text = " + text +
                    "WHERE postID = " + postId,

                    new ResultSetHandler() {
                        @Override
                        public void handleResult(ResultSet rs) {

                        }
                    }
                );
            }
    	} catch (Exception e) {
            e.printStackTrace();
    	}
    }
}
